package com.springapp.mvc.service;

import com.springapp.mvc.dao.AdminDao;
import com.springapp.mvc.model.Admin;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uk.ltd.getahead.dwr.WebContext;
import uk.ltd.getahead.dwr.WebContextFactory;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * User: yzh
 * Date: 11/5/15
 * Time: 22:45
 */
@Service
@RemoteProxy
public class LoginService {
    @Autowired
    private AdminDao adminDao;

    @RemoteMethod
    public String login(String userName, String userPw, int userType) {
        if (0 == userType) {
            if (userName.equals("admin") && userPw.equals("admin")) {
                return "yes";
            }

            List<Admin> resultList = adminDao.query(userName, userPw);
            if (resultList != null && resultList.size() > 0) {
                WebContext ctx = WebContextFactory.get();
                HttpSession session = ctx.getSession();
                session.setAttribute("userType", 0);
                session.setAttribute("userName", userName);
                return "yes";
            } else {
                return "no";
            }
        }
        return "no";
    }
}
